package org.app.dao;

import java.util.HashSet;
import java.util.List;
import java.util.Set;



import org.app.domain.Contact;
import org.hibernate.Hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class ContactDAOImpl implements IContactDAO {
	@Autowired
    private SessionFactory sessionFactory;
	
	
 
	public List<Contact> listContact() {
        List<Contact> list =  sessionFactory.getCurrentSession().createQuery("from Contact")
                .list();
        for(Contact c : list){
        	if(c.getCars().size() > 0)
        		c.getCars();
        }
        return list;
        
    }
 
    public void removeContact(Long id) {
        Contact contact = (Contact) sessionFactory.getCurrentSession().load(
                Contact.class, id);
        if (null != contact) {
            sessionFactory.getCurrentSession().delete(contact);
        }
 
    }

	@Override
	public void addContact(Contact contact) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().save(contact);
		
	}

	@Override
	public Contact selectContactById(Long id) {
		Contact contact = (Contact) sessionFactory.getCurrentSession().get(Contact.class, id);
		return contact;
	}

	@Override
	public Contact updateContact(Contact contact) {
		sessionFactory.getCurrentSession().merge(contact);
		return contact;
	}
	public static <E> Set<E> union(Set<E> s1, Set<E> s2) {
		Set<E> result = new HashSet<E>(s1);
		result.addAll(s2);
		return result;
		}

}
